/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Boundaries.InternalFrame_ubahStatusAdmin;
import Boundaries.InternalFrame_ubahStatusStaff;
import Entities.Kendaraan;
import Entities.Servis;
import Entities.Sewa;
import TableModel.Kendaraan_List;
import TableModel.Kendaraan_List_TableModel;
import TableModel.Persewaan_List;
import TableModel.Persewaan_Rekap_TableModel;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Tea-Nose
 */
public class Change_Controller {

    private void rubahKendaraanStaff(javax.swing.JInternalFrame frame){
        InternalFrame_ubahStatusStaff formUbahStatusStaff=(InternalFrame_ubahStatusStaff) frame;
        if (formUbahStatusStaff.tf_NoSewa.getText().matches("")
                || formUbahStatusStaff.tf_noPolisiKendaraan.getText().matches("")
                || formUbahStatusStaff.tf_tanggalSewa.getText().matches("")
                || formUbahStatusStaff.cmbBox_statusBaru.getSelectedItem().toString().matches("")){
            JOptionPane.showMessageDialog(null, "Error");
        }
    }
    public void tableSewaClicked(JInternalFrame frame){
        InternalFrame_ubahStatusStaff ubh_Staff=(InternalFrame_ubahStatusStaff) frame;
        ubh_Staff.cmbBox_statusBaru.setEnabled(true);
        ubh_Staff.button_OK.setEnabled(true);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yy");

        Date date = new Date();
        int row = ubh_Staff.jTable1.getSelectedRow();
        ubh_Staff.tf_NoSewa.setText(ubh_Staff.jTable1.getValueAt(row, 0).toString());
        ubh_Staff.tf_noPolisiKendaraan.setText(ubh_Staff.jTable1.getValueAt(row, 8).toString());
        ubh_Staff.tf_tanggalSewa.setText(ubh_Staff.jTable1.getValueAt(row, 4).toString());
        ubh_Staff.tf_tanggalKembali.setText(ubh_Staff.jTable1.getValueAt(row, 6).toString());
        ubh_Staff.tf_tanggalSekarang.setText(simpleDateFormat.format(date));
        ubh_Staff.tf_BiayaSewa.setText(ubh_Staff.jTable1.getValueAt(row, 5).toString());

        int keterlambatan = Integer.parseInt(ubh_Staff.jTable1.getValueAt(row, 9).toString());
        if (keterlambatan > 0) {
            int denda = Integer.parseInt(ubh_Staff.tf_BiayaSewa.getText()) * keterlambatan * 10 / 100;
            int totalBayar = denda + Integer.parseInt(ubh_Staff.tf_BiayaSewa.getText());
            ubh_Staff.tf_Denda.setText("" + denda);
            ubh_Staff.tf_TotalBayar.setText("" + totalBayar);

        } else {
            ubh_Staff.tf_Denda.setText("0");
            ubh_Staff.tf_TotalBayar.setText(ubh_Staff.tf_BiayaSewa.getText());
        }
    }

    public void okButtonClicked(JInternalFrame frame){
         InternalFrame_ubahStatusStaff ubh_Staff=(InternalFrame_ubahStatusStaff) frame;

         try {
            // TODO add your handling code here:
            int row = ubh_Staff.jTable1.getSelectedRow();
            int biayaSewa = Integer.parseInt(ubh_Staff.tf_Denda.getText());
            String noPolisi = ubh_Staff.tf_noPolisiKendaraan.getText();
//            String noFaktur=ubh_Staff.jTable1.getValueAt(row, 0).toString();
            String statusKendaraan = ubh_Staff.cmbBox_statusBaru.getSelectedItem().toString();
            Kendaraan kendaraan = new Kendaraan();
            kendaraan.setStatus_kendaraan(statusKendaraan);
            kendaraan.setNo_polisi(noPolisi);
//            try {
//                kendaraan.changeStatusKendaraan();
//            } catch (SQLException ex) {
//                Logger.getLogger(InternalFrame_ubahStatusStaff.class.getName()).log(Level.SEVERE, null, ex);
//            }
            kendaraan.changeStatusKendaraan();
            Sewa sewa = new Sewa();

            sewa.kembaliSewa(biayaSewa, ubh_Staff.tf_NoSewa.getText());
            List<Sewa> sewas = Persewaan_List.getKendaraanList().getAllPersewaan();
            Persewaan_Rekap_TableModel persewaan_Rekap_TableModel = new Persewaan_Rekap_TableModel(sewas);
            ubh_Staff.jTable1.setModel(persewaan_Rekap_TableModel);
            JOptionPane.showMessageDialog(null, "Proses pengembalian berhasil");
        } catch (Exception ex) {
            Logger.getLogger(InternalFrame_ubahStatusStaff.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void okButtonAdminClicked(JInternalFrame frame){
         InternalFrame_ubahStatusAdmin ubh_Staff=(InternalFrame_ubahStatusAdmin) frame;

//         try {
//            // TODO add your handling code here:
//            Servis servis = new Servis();
//            servis.dataServisKembali(Double.parseDouble(ubh_Staff.tf_biayaServis.getText()), params);
//            Kendaraan kendaraan = new Kendaraan();
//            kendaraan.setStatus_kendaraan(ubh_Staff.cmbBox_statusBaru.getSelectedItem().toString());
//            try {
//                kendaraan.setNo_polisi(ubh_Staff.tf_noPolisiKendaraan.getText().toUpperCase());
//            } catch (Exception ex) {
//                Logger.getLogger(InternalFrame_ubahStatusAdmin.class.getName()).log(Level.SEVERE, null, ex);
//            }
//            kendaraan.changeStatusKendaraan();
//
//            List<Kendaraan> kendaraans = Kendaraan_List.getKendaraanList().getAllKendaraanServis();
//            Kendaraan_List_TableModel kendaraan_TableModel = new Kendaraan_List_TableModel(kendaraans);
//            ubh_Staff.jTable1.setModel(kendaraan_TableModel);
//
//            ubh_Staff.tf_noPolisiKendaraan.setText(null);
//            ubh_Staff.tf_merkKendaraan.setText(null);
//            ubh_Staff.tf_statusTerakhir.setText(null);
//            ubh_Staff.cmbBox_statusBaru.setEnabled(false);
//            ubh_Staff.tf_biayaServis.setEditable(false);
//        } catch (SQLException ex) {
//            JOptionPane.showMessageDialog(null, "Terjadi error");
//        } catch (NullPointerException ex) {
//            JOptionPane.showMessageDialog(null, "Terjadi error");
//        } catch (NumberFormatException ex) {
//            JOptionPane.showMessageDialog(null, "Terjadi error");
//        }
    }
}
